//
//  SearchViewController.m
//  MSDemo
//
//  Created by ms on 2023/2/20.
//

#import "SearchViewController.h"

@interface SearchViewController ()<
UITextFieldDelegate,
UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout
>

@property (nonatomic, strong) UITextField *searchBar;
@property (nonatomic, strong) SCSearchSearchBarLeftView *searchBar_leftView;
@property (nonatomic, strong) UIButton *searchBtn;
/** 占位页 */
@property (nonatomic, strong) UIScrollView *placeholderView;
@property (nonatomic, strong) UIView *placeholderView_historyView;
@property (nonatomic, strong) UIView *placeholderView_hotView;
@property (nonatomic, strong) SCSearchGoodsSortView *goodsSortView;
@property (nonatomic, strong) SCSearchStoreSortView *storeSortView;
@property (nonatomic, strong) UICollectionView *storeListCollectionView;
@property (nonatomic, strong) UICollectionView *goodsListCollectionView;
@property (nonatomic, strong) SCSearchSourceView *searchTypeView;
@property (nonatomic, strong) NSMutableArray *historyRecords;
@property (nonatomic, assign) BOOL showAllHistoryRecords;
@property (nonatomic, assign) BOOL isDeleting;

@property (nonatomic, strong) NSArray *hotRecords;
@property (nonatomic, strong) NSArray *sourceArr;
@property (nonatomic, assign) NSInteger number;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navBar.leftTextLabel.text = @"";
    [self.searchBar becomeFirstResponder];
    self.view.backgroundColor = MS_HEX_COLOR(0xF8F8F8);
    self.number = 0;
    
    [self.navBar addSubview:self.searchBar];
    [self.navBar addSubview:self.searchBtn];
    [self.view addSubview:self.placeholderView];
    [self.placeholderView addSubview:self.placeholderView_historyView];
    [self.placeholderView addSubview:self.placeholderView_hotView];
    [self.view addSubview:self.goodsSortView];
    [self.view addSubview:self.goodsListCollectionView];
    [self.view addSubview:self.storeSortView];
    [self.view addSubview:self.storeListCollectionView];
    [self.view insertSubview:self.searchTypeView
                aboveSubview:self.navBar];
    
    [self reloadPlaceholderView_historyViewUI];
    [self reloadPlaceholderView_hotViewUI];
    ms_updateViewLayout(self.view);
}


#pragma mark - event
/** 点击了【搜索】按钮 */
- (void)didSelectedSearchButton
{
    if ([self.searchBar canResignFirstResponder])//能收起键盘
    {
        //收起键盘
        [self.searchBar resignFirstResponder];
    }
    
    if (ms_stringIsEmpty(self.searchBar.text))//未输入内容
    {
        if (ms_stringIsEmpty(self.placeholder))//占位符为空
        {
            self.placeholderView.hidden = NO;
            self.goodsSortView.hidden = YES;
            self.goodsListCollectionView.hidden = YES;
            self.storeSortView.hidden = YES;
            self.storeListCollectionView.hidden = YES;
        }
        else//占位符不为空，搜索占位符
        {
            self.placeholderView.hidden = YES;
            self.number = arc4random() % 10 + 1;
            if ([self.source isEqualToString:@"1"])//搜店铺
            {
                self.goodsSortView.hidden = YES;
                self.goodsListCollectionView.hidden = YES;
                self.storeSortView.hidden = NO;
                self.storeListCollectionView.hidden = NO;
                [self.storeListCollectionView reloadData];
            }
            else
            {
                self.goodsSortView.hidden = NO;
                self.goodsListCollectionView.hidden = NO;
                self.storeSortView.hidden = YES;
                self.storeListCollectionView.hidden = YES;
                [self.goodsListCollectionView reloadData];
            }
            
            if ([self.historyRecords containsObject:self.placeholder])
            {
                [self.historyRecords removeObject:self.placeholder];
            }
            
            [self.historyRecords insertObject:self.placeholder atIndex:0];
            [NSKeyedArchiver archiveRootObject:self.historyRecords
                                        toFile:[ms_pathOfDocument stringByAppendingPathComponent:@"mwxsc_searchHistoriesCache"]];
            [self reloadPlaceholderView_historyViewUI];
        }
    }
    else//已输入内容
    {
        self.placeholderView.hidden = YES;
        self.number = arc4random() % 10 + 1;
        if ([self.source isEqualToString:@"1"])//搜店铺
        {
            self.goodsSortView.hidden = YES;
            self.goodsListCollectionView.hidden = YES;
            self.storeSortView.hidden = NO;
            self.storeListCollectionView.hidden = NO;
            [self.storeListCollectionView reloadData];
        }
        else
        {
            self.goodsSortView.hidden = NO;
            self.goodsListCollectionView.hidden = NO;
            self.storeSortView.hidden = YES;
            self.storeListCollectionView.hidden = YES;
            [self.goodsListCollectionView reloadData];
        }
        
        if ([self.historyRecords containsObject:self.searchBar.text])
        {
            [self.historyRecords removeObject:self.searchBar.text];
        }
        
        [self.historyRecords insertObject:self.searchBar.text atIndex:0];
        [NSKeyedArchiver archiveRootObject:self.historyRecords
                                    toFile:[ms_pathOfDocument stringByAppendingPathComponent:@"mwxsc_searchHistoriesCache"]];
        [self reloadPlaceholderView_historyViewUI];
    }
}
/** 点击了【删除】/【全部删除】按钮*/
- (void)didSelectedDeleteBtn
{
    if (self.isDeleting)//删除中
    {
        //全部删除
        [self.historyRecords removeAllObjects];
        [NSKeyedArchiver archiveRootObject:self.historyRecords
                                    toFile:[ms_pathOfDocument stringByAppendingPathComponent:@"mwxsc_searchHistoriesCache"]];
        self.isDeleting = NO;
        self.showAllHistoryRecords = NO;
    }
    else
    {
        //进入删除状态
        self.isDeleting = YES;
        self.showAllHistoryRecords = YES;
    }
    
    [self reloadPlaceholderView_historyViewUI];
    self.placeholderView_hotView.hidden = self.isDeleting;
    
    if ([self.searchBar canResignFirstResponder])//能收起键盘
    {
        //收起键盘
        [self.searchBar resignFirstResponder];
    }
}
/** 点击了【完成】按钮*/
- (void)didSelectedDoneBtn
{
    self.isDeleting = NO;
    self.showAllHistoryRecords = NO;
    [self reloadPlaceholderView_historyViewUI];
    self.placeholderView_hotView.hidden = self.isDeleting;
    
    if ([self.searchBar canResignFirstResponder])//能收起键盘
    {
        //收起键盘
        [self.searchBar resignFirstResponder];
    }
}
/** 点击了【近期搜索展开/回收】按钮 */
- (void)didSelectedShowAllDeleteButton
{
    self.showAllHistoryRecords = !self.showAllHistoryRecords;
    [self reloadPlaceholderView_historyViewUI];
    
    if ([self.searchBar canResignFirstResponder])//能收起键盘
    {
        //收起键盘
        [self.searchBar resignFirstResponder];
    }
}
/** 点击了【历史搜索词】按钮 */
- (void)didSelectHistoryButton:(UIButton *)sender
{
    NSInteger idx = sender.tag - 1000;
    
    if (self.isDeleting)
    {
        [self.historyRecords removeObjectAtIndex:idx];
        [NSKeyedArchiver archiveRootObject:self.historyRecords
                                    toFile:[ms_pathOfDocument stringByAppendingPathComponent:@"mwxsc_searchHistoriesCache"]];
        [self reloadPlaceholderView_historyViewUI];
    }
    else
    {
        self.searchBar.text = self.historyRecords[idx];
        
        [self didSelectedSearchButton];
    }
    
    if ([self.searchBar canResignFirstResponder])//能收起键盘
    {
        //收起键盘
        [self.searchBar resignFirstResponder];
    }
}
/** 点击了【热门搜索词】按钮 */
- (void)didSelectHotButton:(UIButton *)sender
{
    NSInteger idx = sender.tag - 2000;
    self.searchBar.text = self.hotRecords[idx];
    
    [self didSelectedSearchButton];
    
    if ([self.searchBar canResignFirstResponder])//能收起键盘
    {
        //收起键盘
        [self.searchBar resignFirstResponder];
    }
}


#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.placeholderView.hidden = NO;
    self.goodsSortView.hidden = YES;
    self.goodsListCollectionView.hidden = YES;
    self.storeSortView.hidden = YES;
    self.storeListCollectionView.hidden = YES;
    
    self.searchBar_leftView.showTextView = NO;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (!ms_stringIsEmpty(textField.text))
    {
        self.searchBar_leftView.text = textField.text;
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    self.searchTypeView.hidden = YES;
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self didSelectedSearchButton];
    return YES;
}


#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:self.placeholderView])
    {
        if ([self.searchBar canResignFirstResponder])//能收起键盘
        {
            //收起键盘
            [self.searchBar resignFirstResponder];
            
            self.placeholderView.hidden = NO;
            self.goodsSortView.hidden = YES;
            self.goodsListCollectionView.hidden = YES;
            self.storeSortView.hidden = YES;
            self.storeListCollectionView.hidden = YES;
        }
    }
}


#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}


#pragma mark - MSWaterFlowLayoutDelegate/UICollectionViewDelegateFlowLayout
/** 返回每个item的size */
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([collectionView isEqual:self.storeListCollectionView])
    {
        return CGSizeMake(690 * MS_ADAPTATION_RATIO, 540 * MS_ADAPTATION_RATIO);
    }
    else
    {
        return CGSizeMake(340 * MS_ADAPTATION_RATIO, 580 * MS_ADAPTATION_RATIO);
    }
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.number;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([collectionView isEqual:self.storeListCollectionView])
    {
        UICollectionViewCell*cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SCStoreCollectionViewCell"
                                                                                   forIndexPath:indexPath];
        cell.contentView.backgroundColor = UIColor.redColor;
       return cell;
    }
    else
    {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SCGoodsCollectionViewCell"
                                                                                    forIndexPath:indexPath];
        cell.contentView.backgroundColor = UIColor.blackColor;
        return cell;
    }
}


#pragma mark - UI
- (void)reloadPlaceholderView_historyViewUI
{
    [self.placeholderView_historyView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.placeholderView_historyView.hidden = ms_arrayIsEmpty(self.historyRecords);
    
    if (!ms_arrayIsEmpty(self.historyRecords))
    {
        UILabel *historyLabel = [[UILabel alloc] init];
        historyLabel.font = MS_BOLD_SYSTEM_FONT_OF_SIZE(16);
        historyLabel.text = @"近期搜索";
        historyLabel.textColor = MS_HEX_COLOR(0x323334);
        
        [self.placeholderView_historyView addSubview:historyLabel];
        
        [historyLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(24 * MS_ADAPTATION_RATIO);
            make.left.mas_equalTo(38 * MS_ADAPTATION_RATIO);
        }];
        
        if (self.isDeleting)//删除状态
        {
            UIButton *delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            delBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(12);
            [delBtn setTitle:@"全部删除"
                    forState:UIControlStateNormal];
            [delBtn setTitleColor:MS_HEX_COLOR(0x999999)
                         forState:UIControlStateNormal];
            [delBtn addTarget:self
                       action:@selector(didSelectedDeleteBtn)
             forControlEvents:UIControlEventTouchUpInside];
            [self.placeholderView_historyView addSubview:delBtn];
            
            [delBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.right.mas_equalTo(-100 * MS_ADAPTATION_RATIO);
                make.width.mas_equalTo(112 * MS_ADAPTATION_RATIO);
                make.height.mas_equalTo(52 * MS_ADAPTATION_RATIO);
                make.centerY.equalTo(historyLabel);
            }];
            
            UIView *lineView = [[UIView alloc] init];
            lineView.backgroundColor = MS_HEX_COLOR(0xCDCDCD);
            [self.placeholderView_historyView addSubview:lineView];
            
            [lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.left.equalTo(delBtn.mas_right);
                make.width.mas_equalTo(0.5);
                make.height.mas_equalTo(18 * MS_ADAPTATION_RATIO);
                make.centerY.equalTo(historyLabel);
            }];
            
            UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            doneBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(12);
            [doneBtn setTitle:@"完成"
                    forState:UIControlStateNormal];
            [doneBtn setTitleColor:MS_HEX_COLOR(0xFE3C2E)
                         forState:UIControlStateNormal];
            [doneBtn addTarget:self
                       action:@selector(didSelectedDoneBtn)
             forControlEvents:UIControlEventTouchUpInside];
            [self.placeholderView_historyView addSubview:doneBtn];
            
            [doneBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.right.mas_equalTo(-27 * MS_ADAPTATION_RATIO);
                make.width.mas_equalTo(64 * MS_ADAPTATION_RATIO);
                make.height.mas_equalTo(52 * MS_ADAPTATION_RATIO);
                make.centerY.equalTo(historyLabel);
            }];
        }
        else//非删除状态
        {
            UIButton *delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [delBtn setImage:MS_IMAGE_NAMED(@"delete2")
                    forState:UIControlStateNormal];
            [delBtn addTarget:self
                       action:@selector(didSelectedDeleteBtn)
             forControlEvents:UIControlEventTouchUpInside];
            [self.placeholderView_historyView addSubview:delBtn];
            
            [delBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.right.mas_equalTo(-20 * MS_ADAPTATION_RATIO);
                make.width.height.mas_equalTo(52 * MS_ADAPTATION_RATIO);
                make.centerY.equalTo(historyLabel);
            }];
        }
        
        __block NSInteger row = 0;
        /// 记录【搜索记录】按钮是第一个
        __block BOOL btn_isFirst = YES;
        /// 记录【搜索记录】按钮是最小frame.origin.x
        __block CGFloat last_btn_max_x = 0;//38 * MS_ADAPTATION_RATIO
        CGFloat btn_height = 56 * MS_ADAPTATION_RATIO;
        BOOL show_showAlldelBtn = YES;
        for (int i = 0; i < self.historyRecords.count; i++)
        {
            UIFont *font = MS_SYSTEM_FONT_OF_SIZE(14);
            NSString *title = self.historyRecords[i];
            
            UIControl *control = [[UIControl alloc] init];
            control.backgroundColor = UIColor.whiteColor;
            control.tag = 1000 + i;
            [control ms_makeChains:^(MSChainMaker * _Nonnull make) {
                
                make.ms_cornerRadii(btn_height / 2.0);
            }];
            
            CGFloat title_width = MAX(ms_sizeOfText(title, font).width + 60 * MS_ADAPTATION_RATIO, self.isDeleting ? 112 * MS_ADAPTATION_RATIO : 88 * MS_ADAPTATION_RATIO);
            CGFloat btn_width = MIN(title_width, MS_SCREEN_WIDTH - 76 * MS_ADAPTATION_RATIO);
            [control addTarget:self
                    action:@selector(didSelectHistoryButton:)
          forControlEvents:UIControlEventTouchUpInside];
            
            if ((int)(last_btn_max_x + (last_btn_max_x == 0 ? 38 * MS_ADAPTATION_RATIO : 24 * MS_ADAPTATION_RATIO) + btn_width) > (int)(MS_SCREEN_WIDTH - 38 * MS_ADAPTATION_RATIO))//超出范围
            {
                btn_isFirst = YES;
                
                if (!self.showAllHistoryRecords &&//非显示全部历史搜索记录状态
                    i > 0 &&
                    (int)(38 * MS_ADAPTATION_RATIO + btn_width + 24 * MS_ADAPTATION_RATIO + 58 * MS_ADAPTATION_RATIO) > (int)(MS_SCREEN_WIDTH - 38 * MS_ADAPTATION_RATIO))//单行无法同时容纳 btn和showAlldelBtn 按钮
                {
                    show_showAlldelBtn = YES;
                    break;
                }
                
                //向下重启一行
                row ++;
                last_btn_max_x = 0;
                show_showAlldelBtn = row > 1;
                
                [self.placeholderView_historyView addSubview:control];
                
                [control mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.mas_equalTo(93 * MS_ADAPTATION_RATIO + row * (24 * MS_ADAPTATION_RATIO + btn_height));
                    make.left.mas_equalTo(last_btn_max_x == 0 ? 38 * MS_ADAPTATION_RATIO : last_btn_max_x + 24 * MS_ADAPTATION_RATIO);
                    make.bottom.mas_lessThanOrEqualTo(-16 * MS_ADAPTATION_RATIO);
                    make.width.mas_equalTo(btn_width);
                    make.height.mas_equalTo(btn_height);
                }];
                last_btn_max_x = ((last_btn_max_x == 0 ? 38 * MS_ADAPTATION_RATIO : last_btn_max_x + 24 * MS_ADAPTATION_RATIO) + btn_width);
            }
            else//未超出范围
            {
                show_showAlldelBtn = row > 1;
                
                if (!self.showAllHistoryRecords &&//非显示全部历史搜索记录状态
                    i > 0 &&
                    (int)(last_btn_max_x + (last_btn_max_x == 0 ? 38 * MS_ADAPTATION_RATIO : 24 * MS_ADAPTATION_RATIO) + btn_width + 24 * MS_ADAPTATION_RATIO + 58 * MS_ADAPTATION_RATIO) > (int)(MS_SCREEN_WIDTH - 38 * MS_ADAPTATION_RATIO))//当前行无法继续同时容纳 btn和showAlldelBtn 按钮
                {
                    show_showAlldelBtn = YES;
                    break;
                }
                
                [self.placeholderView_historyView addSubview:control];
                
                [control mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.mas_equalTo(93 * MS_ADAPTATION_RATIO + row * (24 * MS_ADAPTATION_RATIO + btn_height));
                    make.left.mas_equalTo(last_btn_max_x == 0 ? 38 * MS_ADAPTATION_RATIO : last_btn_max_x + 24 * MS_ADAPTATION_RATIO);
                    make.bottom.mas_lessThanOrEqualTo(-16 * MS_ADAPTATION_RATIO);
                    make.width.mas_equalTo(btn_width);
                    make.height.mas_equalTo(btn_height);
                }];
                last_btn_max_x = ((last_btn_max_x == 0 ? 38 * MS_ADAPTATION_RATIO : last_btn_max_x + 24 * MS_ADAPTATION_RATIO) + btn_width);
            }
            
            if (self.isDeleting)//删除状态
            {
                UIView *superView = [[UIView alloc] init];
                superView.userInteractionEnabled = NO;
                [control addSubview:superView];
                
                [superView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.equalTo(control);
                    make.left.mas_greaterThanOrEqualTo(12 * MS_ADAPTATION_RATIO);
                    make.center.equalTo(control);
                }];
                
                UILabel *label = [[UILabel alloc] init];
                label.font = MS_SYSTEM_FONT_OF_SIZE(14);
                label.lineBreakMode = NSLineBreakByTruncatingMiddle;
                label.text = title;
                label.textColor = MS_HEX_COLOR(0x333333);
                
                [superView addSubview:label];
                
                [label mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.left.equalTo(superView);
                    make.centerY.equalTo(superView);
                }];
                
                UIView *iconView = [[UIView alloc] init];
                iconView.layer.contents = (id)MS_IMAGE_NAMED(@"delete3").CGImage;
                
                [superView addSubview:iconView];
                
                [iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.left.equalTo(label.mas_right).offset(8 * MS_ADAPTATION_RATIO);
                    make.right.equalTo(superView);
                    make.width.height.mas_equalTo(16 * MS_ADAPTATION_RATIO);
                    make.centerY.equalTo(superView);
                }];
            }
            else//非删除状态
            {
                UILabel *label = [[UILabel alloc] init];
                label.font = MS_SYSTEM_FONT_OF_SIZE(14);
                label.lineBreakMode = NSLineBreakByTruncatingMiddle;
                label.text = title;
                label.textColor = MS_HEX_COLOR(0x333333);
                label.textAlignment = NSTextAlignmentCenter;
                
                [control addSubview:label];
                
                [label mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.equalTo(control);
                    make.left.mas_greaterThanOrEqualTo(12 * MS_ADAPTATION_RATIO);
                    make.center.equalTo(control);
                }];
            }
        }
        
        if (show_showAlldelBtn)//显示【展开/收起】按钮
        {
            if ((int)(last_btn_max_x + 24 * MS_ADAPTATION_RATIO + 58 * MS_ADAPTATION_RATIO) > (int)(MS_SCREEN_WIDTH - 38 * MS_ADAPTATION_RATIO))//超出范围
            {
                //向下重启一行
                row ++;
                last_btn_max_x = 0;
            }
            
            UIButton *showAlldelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            showAlldelBtn.backgroundColor = UIColor.whiteColor;
            UIImage *img = [MS_IMAGE_NAMED(@"arrow_down") ms_scaleToSize:CGSizeMake(22 * MS_ADAPTATION_RATIO, 13 * MS_ADAPTATION_RATIO)];
            if (self.showAllHistoryRecords)
            {
                img = [img ms_rotateImageWithDegree:180];
            }
            [showAlldelBtn setImage:img
                    forState:UIControlStateNormal];
            [showAlldelBtn ms_makeChains:^(MSChainMaker * _Nonnull make) {
                
                make.ms_cornerRadii(btn_height / 2.0);
            }];
            [showAlldelBtn addTarget:self
                       action:@selector(didSelectedShowAllDeleteButton)
             forControlEvents:UIControlEventTouchUpInside];
            [self.placeholderView_historyView addSubview:showAlldelBtn];
            
            [showAlldelBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.top.mas_equalTo(93 * MS_ADAPTATION_RATIO + row * (24 * MS_ADAPTATION_RATIO + btn_height));
                make.left.mas_equalTo(last_btn_max_x == 0 ? 38 * MS_ADAPTATION_RATIO : last_btn_max_x + 24 * MS_ADAPTATION_RATIO);
                make.bottom.mas_lessThanOrEqualTo(-16 * MS_ADAPTATION_RATIO);
                make.width.mas_equalTo(58 * MS_ADAPTATION_RATIO);
                make.height.mas_equalTo(btn_height);
            }];
        }
    }
}
- (void)reloadPlaceholderView_hotViewUI
{
    [self.placeholderView_hotView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.placeholderView_hotView.hidden = ms_arrayIsEmpty(self.hotRecords);
    
    if (!ms_arrayIsEmpty(self.hotRecords))
    {
        UILabel *hotLabel = [[UILabel alloc] init];
        hotLabel.font = MS_BOLD_SYSTEM_FONT_OF_SIZE(16);
        hotLabel.text = @"热门搜索";
        hotLabel.textColor = MS_HEX_COLOR(0x323334);
        
        [self.placeholderView_hotView addSubview:hotLabel];
        
        [hotLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(24 * MS_ADAPTATION_RATIO);
            make.left.mas_equalTo(38 * MS_ADAPTATION_RATIO);
        }];
        
        __block NSInteger row = 0;
        /// 记录【搜索记录】按钮是最小frame.origin.x
        __block CGFloat last_btn_max_x = 0;
        CGFloat btn_height = 67 * MS_ADAPTATION_RATIO;
        for (int i = 0; i < self.hotRecords.count; i++)
        {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.backgroundColor = UIColor.whiteColor;
            btn.tag = 2000 + i;
            btn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(14);
            btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
            [btn setTitle:self.hotRecords[i]
                 forState:UIControlStateNormal];
            [btn setTitleColor:MS_HEX_COLOR(0x333333)
                      forState:UIControlStateNormal];
            btn.contentEdgeInsets = UIEdgeInsetsMake(0, 30 * MS_ADAPTATION_RATIO, 0, 30 * MS_ADAPTATION_RATIO);
            [btn ms_makeChains:^(MSChainMaker * _Nonnull make) {
                
                if (self.isDeleting)
                {
                    make.ms_buttonType(MSButtonModelTextAndImgHorizontal);
                    make.ms_space(8 * MS_ADAPTATION_RATIO);
                }
                make.ms_cornerRadii(btn_height / 2.0);
            }];
            
            CGFloat title_width = MAX(ms_sizeOfText([btn titleForState:UIControlStateNormal], btn.titleLabel.font).width + 60 * MS_ADAPTATION_RATIO, 88 * MS_ADAPTATION_RATIO);
            CGFloat btn_width = MIN(title_width, MS_SCREEN_WIDTH - 76 * MS_ADAPTATION_RATIO);
            [btn addTarget:self
                    action:@selector(didSelectHotButton:)
          forControlEvents:UIControlEventTouchUpInside];
            
            if ((int)(last_btn_max_x + (last_btn_max_x == 0 ? 38 * MS_ADAPTATION_RATIO : 24 * MS_ADAPTATION_RATIO) + btn_width) > (int)(MS_SCREEN_WIDTH - 38 * MS_ADAPTATION_RATIO))//超出范围
            {
                //向下重启一行
                row ++;
                last_btn_max_x = 0;
            }
            
            [self.placeholderView_hotView addSubview:btn];
            
            [btn mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.top.mas_equalTo(93 * MS_ADAPTATION_RATIO + row * (24 * MS_ADAPTATION_RATIO + btn_height));
                make.left.mas_equalTo(last_btn_max_x == 0 ? 38 * MS_ADAPTATION_RATIO : last_btn_max_x + 24 * MS_ADAPTATION_RATIO);
                make.bottom.mas_lessThanOrEqualTo(-16 * MS_ADAPTATION_RATIO);
                make.width.mas_equalTo(btn_width);
                make.height.mas_equalTo(btn_height);
            }];
            last_btn_max_x = ((last_btn_max_x == 0 ? 38 * MS_ADAPTATION_RATIO : last_btn_max_x + 24 * MS_ADAPTATION_RATIO) + btn_width);
        }
    }
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    UIView *view = [[UIView alloc] initWithFrame:self.searchBar.bounds];
    [view ms_makeChains:^(MSChainMaker * _Nonnull make) {
        
        make.ms_startPoint(CGPointZero);
        make.ms_endPoint(CGPointMake(1, 0));
        make.ms_colors(@[MS_HEX_COLOR(0xCC0047), MS_HEX_COLOR(0xFAD05B), MS_HEX_COLOR(0xCC0047)]);
    }];
    UIColor *color = [UIColor colorWithPatternImage:[UIImage ms_imageWithView:view withSize:view.bounds.size opaque:NO]];
    [self.searchBar ms_makeChains:^(MSChainMaker * _Nonnull make) {
        
        make.ms_cornerRadii(view.bounds.size.height / 2.0);
        make.ms_lineColor(color);
        make.ms_lineWidth(2);
    }];
}
- (void)updateViewConstraints
{
    [super updateViewConstraints];
    [self.searchBar mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(45);
        make.bottom.mas_equalTo(-7);
        make.right.mas_equalTo(-55);
        make.height.mas_equalTo(30);
    }];
    [self.searchBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.searchBar.mas_right);
        make.right.mas_equalTo(-3);
        make.height.mas_equalTo(30);
        make.centerY.equalTo(self.searchBar);
    }];
    [self.placeholderView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.navBar.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.mas_equalTo(-MS_SAFE_AREA_BOTTOM_HEIGHT);
    }];
    [self.placeholderView_historyView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.equalTo(self.placeholderView);
        make.width.equalTo(self.placeholderView);
    }];
    [self.placeholderView_hotView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.placeholderView_historyView.mas_bottom);
        make.left.bottom.right.equalTo(self.placeholderView);
    }];
    [self.goodsSortView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.equalTo(self.placeholderView);
        make.height.mas_equalTo(83 * MS_ADAPTATION_RATIO);
    }];
    [self.goodsListCollectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.goodsSortView.mas_bottom);
        make.left.bottom.right.equalTo(self.placeholderView);
    }];
    [self.storeSortView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.equalTo(self.placeholderView);
        make.height.mas_equalTo(83 * MS_ADAPTATION_RATIO);
    }];
    [self.storeListCollectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.storeSortView.mas_bottom);
        make.left.bottom.right.equalTo(self.placeholderView);
    }];
    [self.searchTypeView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
}

#pragma mark - getter
- (UITextField *)searchBar
{
    if (!_searchBar)
    {
        _searchBar = [[UITextField alloc] init];
        _searchBar.backgroundColor = UIColor.whiteColor;
        _searchBar.delegate = self;
        _searchBar.returnKeyType = UIReturnKeySearch;
        _searchBar.placeholder = self.placeholder;
        @weakify(self);
        RAC(_searchBar, textColor) = [RACObserve(self.searchBar_leftView, showTextView) map:^id _Nullable(id  _Nullable value) {
            
            @strongify(self);
            return self.searchBar_leftView.showTextView ? UIColor.clearColor : MS_HEX_COLOR(0xCAC9C9);
        }];
        _searchBar.font = MS_SYSTEM_FONT_OF_SIZE(14);
        _searchBar.clearButtonMode = UITextFieldViewModeWhileEditing;
        _searchBar.leftViewMode = UITextFieldViewModeAlways;
        _searchBar.leftView = self.searchBar_leftView;
    }
    
    return _searchBar;
}
- (SCSearchSearchBarLeftView *)searchBar_leftView
{
    if (!_searchBar_leftView)
    {
        _searchBar_leftView = [[SCSearchSearchBarLeftView alloc] initWithFrame:CGRectMake(0, 0, CGFLOAT_MAX, 30)];
        _searchBar_leftView.sourceInfo = self.sourceArr.firstObject;
        _searchBar_leftView.showTypeButton = YES;
        @weakify(self);
        _searchBar_leftView.didSelectedTypeButtonBlock = ^(SCSearchSearchBarLeftView * _Nonnull view, NSDictionary * _Nonnull sourceInfo) {
            
            @strongify(self);
            self.searchTypeView.hidden = !self.searchTypeView.isHidden;
            [self.searchTypeView reloadData];
            self.searchBar_leftView.selectingTypeButton = !self.searchTypeView.isHidden;
        };
        _searchBar_leftView.didSelectedTextLabelBlock = ^BOOL(SCSearchSearchBarLeftView * _Nonnull view, NSDictionary * _Nonnull sourceInfo, NSString * _Nonnull text) {
            
            @strongify(self);
            BOOL canBecomeFirstResponder = [self.searchBar canBecomeFirstResponder];
            if (canBecomeFirstResponder)
            {
                self.searchBar.text = text;
                [self.searchBar becomeFirstResponder];
            }
            
            return canBecomeFirstResponder;
        };
        _searchBar_leftView.didSelectedClearButtonBlock = ^(SCSearchSearchBarLeftView * _Nonnull view, NSDictionary * _Nonnull sourceInfo) {
            
            @strongify(self);
            if ([self.searchBar canBecomeFirstResponder])
            {
                self.searchBar.text = @"";
                [self.searchBar becomeFirstResponder];
            }
        };
    }
    
    return _searchBar_leftView;
}
- (UIButton *)searchBtn
{
    if (!_searchBtn)
    {
        _searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _searchBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(14);
        [_searchBtn setTitle:@"搜索"
                    forState:UIControlStateNormal];
        [_searchBtn setTitleColor:MS_HEX_COLOR(0x333333)
                         forState:UIControlStateNormal];
        [_searchBtn addTarget:self
                       action:@selector(didSelectedSearchButton)
             forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _searchBtn;
}
- (UIScrollView *)placeholderView
{
    if (!_placeholderView)
    {
        _placeholderView = [[UIScrollView alloc] init];
        _placeholderView.backgroundColor = UIColor.clearColor;
        _placeholderView.delegate = self;
        _placeholderView.showsVerticalScrollIndicator = NO;
    }
    
    return _placeholderView;
}
- (UIView *)placeholderView_historyView
{
    if (!_placeholderView_historyView)
    {
        _placeholderView_historyView = [[UIView alloc] init];
        _placeholderView_historyView.backgroundColor = UIColor.clearColor;
    }
    
    return _placeholderView_historyView;
}
- (UIView *)placeholderView_hotView
{
    if (!_placeholderView_hotView)
    {
        _placeholderView_hotView = [[UIView alloc] init];
        _placeholderView_hotView.backgroundColor = UIColor.clearColor;
    }
    
    return _placeholderView_hotView;
}
- (SCSearchGoodsSortView *)goodsSortView
{
    if (!_goodsSortView)
    {
        _goodsSortView = [[SCSearchGoodsSortView alloc] init];
        _goodsSortView.hidden = YES;
    }
    
    return _goodsSortView;
}
- (UICollectionView *)goodsListCollectionView
{
    if (!_goodsListCollectionView)
    {
        UICollectionViewFlowLayout*layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 16 * MS_ADAPTATION_RATIO;
        layout.minimumInteritemSpacing = 18 * MS_ADAPTATION_RATIO;
        layout.sectionInset = UIEdgeInsetsMake(0, 26 * MS_ADAPTATION_RATIO, 0, 26 * MS_ADAPTATION_RATIO);
        
        _goodsListCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _goodsListCollectionView.delegate = self;
        _goodsListCollectionView.dataSource = self;
        _goodsListCollectionView.backgroundColor = UIColor.clearColor;
        _goodsListCollectionView.showsVerticalScrollIndicator = NO;
        _goodsListCollectionView.hidden = YES;
        [_goodsListCollectionView registerClass:UICollectionViewCell.class
            forCellWithReuseIdentifier:@"SCGoodsCollectionViewCell"];
    }
    
    return _goodsListCollectionView;
}
- (SCSearchStoreSortView *)storeSortView
{
    if (!_storeSortView)
    {
        _storeSortView = [[SCSearchStoreSortView alloc] init];
        _storeSortView.hidden = YES;
    }
    
    return _storeSortView;
}
- (UICollectionView *)storeListCollectionView
{
    if (!_storeListCollectionView)
    {
        UICollectionViewFlowLayout*layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 16 * MS_ADAPTATION_RATIO;
        layout.minimumInteritemSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(0, 30 * MS_ADAPTATION_RATIO, 0, 30 * MS_ADAPTATION_RATIO);
        
        _storeListCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _storeListCollectionView.delegate = self;
        _storeListCollectionView.dataSource = self;
        _storeListCollectionView.backgroundColor = UIColor.clearColor;
        _storeListCollectionView.showsVerticalScrollIndicator = NO;
        _storeListCollectionView.hidden = YES;
        [_storeListCollectionView registerClass:UICollectionViewCell.class
            forCellWithReuseIdentifier:@"SCStoreCollectionViewCell"];
    }
    
    return _storeListCollectionView;
}
- (SCSearchSourceView *)searchTypeView
{
    if (!_searchTypeView)
    {
        _searchTypeView = [[SCSearchSourceView alloc] init];
        _searchTypeView.hidden = YES;
        
        @weakify(self);
        _searchTypeView.numberOfItemsInSourceView = ^NSInteger(SCSearchSourceView * _Nonnull view) {
            
            @strongify(self);
            return self.sourceArr.count;
        };
        _searchTypeView.titleOfItemsInSourceView = ^NSString * _Nonnull(SCSearchSourceView * _Nonnull view, NSInteger idx) {
            
            @strongify(self);
            NSDictionary *info = self.sourceArr[idx];
            return info[@"title"];
        };
        _searchTypeView.didSelectedItem = ^(SCSearchSourceView * _Nonnull view, NSInteger idx) {
            
            @strongify(self);
            NSDictionary *info = self.sourceArr[idx];
            self.source = info[@"source"];
            self.searchBar_leftView.selectingTypeButton = NO;
            self.searchBar_leftView.sourceInfo = info;
            
            if (self.placeholderView.isHidden)
            {
                self.number = arc4random() % 10 + 1;
                if ([self.source isEqualToString:@"1"])//搜店铺
                {
                    self.goodsSortView.hidden = YES;
                    self.goodsListCollectionView.hidden = YES;
                    self.storeSortView.hidden = NO;
                    self.storeListCollectionView.hidden = NO;
                    [self.storeListCollectionView reloadData];
                }
                else
                {
                    self.goodsSortView.hidden = NO;
                    self.goodsListCollectionView.hidden = NO;
                    self.storeSortView.hidden = YES;
                    self.storeListCollectionView.hidden = YES;
                    [self.goodsListCollectionView reloadData];
                }
            }
        };
        _searchTypeView.sourceViewIsHidden = ^(SCSearchSourceView * _Nonnull view) {
            
            @strongify(self);
            self.searchBar_leftView.selectingTypeButton = NO;
        };
    }
    
    return _searchTypeView;
}
- (NSMutableArray *)historyRecords
{
    if (!_historyRecords)
    {
        NSArray *list = [NSKeyedUnarchiver unarchiveObjectWithFile:[ms_pathOfDocument stringByAppendingPathComponent:@"mwxsc_searchHistoriesCache"]];
        _historyRecords = [NSMutableArray arrayWithArray:list ?: @[]];
    }
    
    return _historyRecords;
}
- (NSArray *)hotRecords
{
    if (!_hotRecords)
    {
        _hotRecords = @[@"电水壶", @"日历", @"适用于苹果快充18w充电器", @"伞", @"日历"];
    }
    
    return _hotRecords;
}
- (NSArray *)sourceArr
{
    if (!_sourceArr)
    {
        _sourceArr = @[@{@"title" : @"商品", @"source" : @"0"},
                       @{@"title" : @"店铺", @"source" : @"1"}];
    }
    
    return _sourceArr;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


#pragma mark - 搜索资源view

@interface SCSearchSourceView ()

@property (nonatomic, strong) UIImageView *contentView;

@end

@implementation SCSearchSourceView

- (void)reloadData
{
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    NSInteger number = self.numberOfItemsInSourceView ? self.numberOfItemsInSourceView(self) : 0;
    
    for (int i = 0; i < number; i++)
    {
        NSString *title = self.titleOfItemsInSourceView ? self.titleOfItemsInSourceView(self, i) : @"";
        UIFont *font = self.titleFontOfItemsInSourceView ? self.titleFontOfItemsInSourceView(self, i) : MS_SYSTEM_FONT_OF_SIZE(14);
        UIFont *selectedFont = self.selectedTitleFontOfItemsInSourceView ? self.selectedTitleFontOfItemsInSourceView(self, i) : MS_SYSTEM_FONT_OF_SIZE(14);
        UIColor *color = self.titleColorOfItemsInSourceView ? self.titleColorOfItemsInSourceView(self, i) : MS_HEX_COLOR(0x333333);
        UIColor *selectedColor = self.selectedTitleColorOfItemsInSourceView ? self.selectedTitleColorOfItemsInSourceView(self, i) : MS_HEX_COLOR(0xEE3234);
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 100 + i;
        RAC(btn.titleLabel, font) = [RACObserve(btn, selected) map:^id _Nullable(id  _Nullable value) {
            
            return [value boolValue] ? selectedFont : font;
        }];
        [btn setTitleColor:color
                  forState:UIControlStateNormal];
        [btn setTitleColor:selectedColor
                  forState:UIControlStateSelected];
        [btn setTitle:title
             forState:UIControlStateNormal];
        btn.selected = i == self.selectedIdx;
        [btn addTarget:self
                action:@selector(didSelectedSourceButton:)
      forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:btn];
        
        [btn mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(15 * MS_ADAPTATION_RATIO + i * 76 * MS_ADAPTATION_RATIO);
            make.left.right.equalTo(self.contentView);
            make.bottom.mas_lessThanOrEqualTo(-12 * MS_ADAPTATION_RATIO);
            make.height.mas_equalTo(76 * MS_ADAPTATION_RATIO);
        }];
    }
}


#pragma mark - event
- (void)didSelectedSourceButton:(UIButton *)sender
{
    NSInteger idx = sender.tag - 100;
    UIButton *oldButton = [self viewWithTag:100 + self.selectedIdx];
    oldButton.selected = NO;
    sender.selected = YES;
    self.selectedIdx = idx;
    
    if (self.didSelectedItem)
    {
        self.didSelectedItem(self, self.selectedIdx);
        
        self.hidden = YES;
        if (self.sourceViewIsHidden)
        {
            self.sourceViewIsHidden(self);
        }
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [touches.allObjects.firstObject locationInView:self];
    if (!CGRectContainsPoint(self.contentView.frame, point))
    {
        self.hidden = YES;
        if (self.sourceViewIsHidden)
        {
            self.sourceViewIsHidden(self);
        }
    }
}


#pragma mark - UI
- (void)updateConstraints
{
    [super updateConstraints];
    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(MS_NAV_BAR_HEIGHT - 2);
        make.left.mas_equalTo(52);
        make.width.mas_equalTo(138 * MS_ADAPTATION_RATIO);
    }];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.selectedIdx = 0;
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        [self addSubview:self.contentView];
    }
    return self;
}


#pragma mark - getter
- (UIImageView *)contentView
{
    if (!_contentView)
    {
        _contentView = [[UIImageView alloc] initWithImage:MS_IMAGE_NAMED(@"search_source_bg")];
        _contentView.userInteractionEnabled = YES;
    }
    
    return _contentView;
}

@end


#pragma mark - 排序(综合、销量降序、价格升序、价格降序) + 自营过滤
@interface SCSearchGoodsSortView ()

/** 综合排序 */
@property (nonatomic, strong) UIButton *zhBtn;
/** 价格排序 */
@property (nonatomic, strong) UIButton *jgBtn;
/** 销量排序 */
@property (nonatomic, strong) UIButton *xlBtn;
/** 自营过滤 */
@property (nonatomic, strong) UIButton *selfSupportBtn;

@end

@implementation SCSearchGoodsSortView

- (void)setSortStatus:(SCSortStatus)sortStatus
{
    _sortStatus = sortStatus;
    
    self.zhBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(14);
    [self.zhBtn setTitleColor:MS_HEX_COLOR(0x666666)
                     forState:UIControlStateNormal];
    
    self.xlBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(14);
    [self.xlBtn setTitleColor:MS_HEX_COLOR(0x666666)
                     forState:UIControlStateNormal];
    
    self.jgBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(14);
    [self.jgBtn setTitleColor:MS_HEX_COLOR(0x666666)
                     forState:UIControlStateNormal];
    [self.jgBtn setImage:[UIImage imageNamed:@"sort"]
                forState:UIControlStateNormal];
    
    self.selfSupportBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(14);
    [self.selfSupportBtn setTitleColor:MS_HEX_COLOR(0x666666)
                              forState:UIControlStateNormal];
    
    switch (_sortStatus)
    {
        case SCSortStatusDefault:
        {
            self.zhBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(15);
            [self.zhBtn setTitleColor:MS_HEX_COLOR(0xF72F62)
                             forState:UIControlStateNormal];
        }
            break;
        case SCSortStatusSalesAscending:
        {
            self.xlBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(15);
            [self.xlBtn setTitleColor:MS_HEX_COLOR(0xF72F62)
                             forState:UIControlStateNormal];
        }
            break;
        case SCSortStatusSalesDescending:
        {
            self.xlBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(15);
            [self.xlBtn setTitleColor:MS_HEX_COLOR(0xF72F62)
                             forState:UIControlStateNormal];
        }
            break;
        case SCSortStatusPriceAscending:
        {
            self.jgBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(15);
            [self.jgBtn setTitleColor:MS_HEX_COLOR(0xF72F62)
                             forState:UIControlStateNormal];
            [self.jgBtn setImage:[UIImage imageNamed:@"sort_ascending"]
                        forState:UIControlStateNormal];
        }
            break;
        case SCSortStatusPriceDescending:
        {
            self.jgBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(15);
            [self.jgBtn setTitleColor:MS_HEX_COLOR(0xF72F62)
                             forState:UIControlStateNormal];
            [self.jgBtn setImage:[UIImage imageNamed:@"sort_descending"]
                        forState:UIControlStateNormal];
        }
            break;
        case SCSortStatusSelfSupport:
        {
            self.selfSupportBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(15);
            [self.selfSupportBtn setTitleColor:MS_HEX_COLOR(0xF72F62)
                                      forState:UIControlStateNormal];
        }
            break;
        default:
            break;
    }
}


#pragma mark - event
/** 点击了【综合】按钮 */
- (void)zhBtnEvent
{
    if (self.sortStatus != SCSortStatusDefault)
    {
        self.sortStatus = SCSortStatusDefault;
        if (self.completeBlock)
        {
            self.completeBlock(self.sortStatus);
        }
    }
}
/** 点击了【销量】按钮 */
- (void)xlBtnEvent
{
    if (self.sortStatus != SCSortStatusSalesDescending)
    {
        self.sortStatus = SCSortStatusSalesDescending;
        if (self.completeBlock)
        {
            self.completeBlock(self.sortStatus);
        }
    }
}
/** 点击了【价格】按钮 */
- (void)jgBtnEvent
{
    if (self.sortStatus == SCSortStatusPriceAscending)//价格升序
    {
        self.sortStatus = SCSortStatusPriceDescending;
    }
    else//价格降序 || 非价格排序
    {
        self.sortStatus = SCSortStatusPriceAscending;
    }
    
    if (self.completeBlock)
    {
        self.completeBlock(self.sortStatus);
    }
}
/** 自营 */
- (void)selfSupportBtnEvent
{
    if (self.sortStatus != SCSortStatusSelfSupport)
    {
        self.sortStatus = SCSortStatusSelfSupport;
        
        if (self.completeBlock)
        {
            self.completeBlock(self.sortStatus);
        }
    }
}


#pragma mark - UI
- (void)updateConstraints
{
    [super updateConstraints];
    [self.zhBtn mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.bottom.equalTo(self);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(120 *MS_ADAPTATION_RATIO);
    }];
    [self.xlBtn mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.bottom.mas_equalTo(self);
        make.left.mas_equalTo(self.zhBtn.mas_right).offset(94 * MS_ADAPTATION_RATIO);
        make.width.mas_equalTo(120 *MS_ADAPTATION_RATIO);
    }];
    [self.jgBtn mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.bottom.mas_equalTo(self);
        make.left.mas_equalTo(self.xlBtn.mas_right).offset(94 * MS_ADAPTATION_RATIO);
        make.width.mas_equalTo(120 *MS_ADAPTATION_RATIO);
    }];
    [self.selfSupportBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(self);
        make.right.mas_equalTo(0);
        make.width.mas_equalTo(120 *MS_ADAPTATION_RATIO);
    }];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = UIColor.clearColor;
        [self addSubview:self.zhBtn];
        [self addSubview:self.xlBtn];
        [self addSubview:self.jgBtn];
        [self addSubview:self.selfSupportBtn];
    }
    
    return self;
}


#pragma mark - lazy loading

- (UIButton *)zhBtn
{
    if (!_zhBtn)
    {
        _zhBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _zhBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(15);
        [_zhBtn setTitle:@"综合"
                forState:UIControlStateNormal];
        [_zhBtn setTitleColor:MS_HEX_COLOR(0xF72F62)
                     forState:UIControlStateNormal];
        [_zhBtn addTarget:self
                   action:@selector(zhBtnEvent)
         forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _zhBtn;
}
- (UIButton *)xlBtn
{
    if (!_xlBtn)
    {
        _xlBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _xlBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(14);
        [_xlBtn setTitle:@"销量"
                forState:UIControlStateNormal];
        [_xlBtn setTitleColor:MS_HEX_COLOR(0x666666)
                     forState:UIControlStateNormal];
        [_xlBtn addTarget:self
                   action:@selector(xlBtnEvent)
         forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _xlBtn;
}
- (UIButton *)jgBtn
{
    if (!_jgBtn)
    {
        _jgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_jgBtn addTarget:self
                   action:@selector(jgBtnEvent)
         forControlEvents:UIControlEventTouchUpInside];
        _jgBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(14);
        [_jgBtn setTitle:@"价格"
                forState:UIControlStateNormal];
        [_jgBtn setImage:[UIImage imageNamed:@"sort"]
                forState:UIControlStateNormal];
        [_jgBtn setTitleColor:MS_HEX_COLOR(0x666666)
                     forState:UIControlStateNormal];
        [_jgBtn ms_makeChains:^(MSChainMaker * _Nonnull make) {
            
            make.ms_buttonType(MSButtonModelTextAndImgHorizontal);
            make.ms_space(4 *MS_ADAPTATION_RATIO);
        }];
    }
    
    return _jgBtn;
}
- (UIButton *)selfSupportBtn
{
    if (!_selfSupportBtn)
    {
        _selfSupportBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _selfSupportBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(14);
        [_selfSupportBtn setTitle:@"自营"
                         forState:UIControlStateNormal];
        [_selfSupportBtn setTitleColor:MS_HEX_COLOR(0x666666)
                              forState:UIControlStateNormal];
        [_selfSupportBtn addTarget:self
                            action:@selector(selfSupportBtnEvent)
                  forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _selfSupportBtn;
}

@end


#pragma mark - 综合排序 + 自营过滤
@interface SCSearchStoreSortView ()

/** 综合排序 */
@property (nonatomic, strong) UIButton *zhBtn;
/** 自营过滤 */
@property (nonatomic, strong) UIButton *selfSupportBtn;

@end

@implementation SCSearchStoreSortView

- (void)setSortStatus:(SCSortStatus)sortStatus
{
    _sortStatus = sortStatus;
    
    self.zhBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(14);
    [self.zhBtn setTitleColor:MS_HEX_COLOR(0x666666)
                     forState:UIControlStateNormal];
    
    self.selfSupportBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(14);
    [self.selfSupportBtn setTitleColor:MS_HEX_COLOR(0x666666)
                              forState:UIControlStateNormal];
    
    switch (_sortStatus)
    {
        case SCSortStatusDefault:
        {
            self.zhBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(15);
            [self.zhBtn setTitleColor:MS_HEX_COLOR(0xF72F62)
                             forState:UIControlStateNormal];
        }
            break;
        case SCSortStatusSelfSupport:
        {
            self.selfSupportBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(15);
            [self.selfSupportBtn setTitleColor:MS_HEX_COLOR(0xF72F62)
                                      forState:UIControlStateNormal];
        }
            break;
        default:
            break;
    }
}

#pragma mark - event
/** 点击了【综合】按钮 */
- (void)zhBtnEvent
{
    if (self.sortStatus != SCSortStatusDefault)
    {
        self.sortStatus = SCSortStatusDefault;
        if (self.completeBlock)
        {
            self.completeBlock(self.sortStatus);
        }
    }
}
/** 自营 */
- (void)selfSupportBtnEvent
{
    if (self.sortStatus != SCSortStatusSelfSupport)
    {
        self.sortStatus = SCSortStatusSelfSupport;
        if (self.completeBlock)
        {
            self.completeBlock(self.sortStatus);
        }
    }
}


#pragma mark - UI
- (void)updateConstraints
{
    [super updateConstraints];
    [self.zhBtn mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.bottom.equalTo(self);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(120 *MS_ADAPTATION_RATIO);
    }];
    [self.selfSupportBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(self);
        make.left.mas_equalTo(214 *MS_ADAPTATION_RATIO);
        make.width.mas_equalTo(120 *MS_ADAPTATION_RATIO);
    }];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = UIColor.clearColor;
        [self addSubview:self.zhBtn];
        [self addSubview:self.selfSupportBtn];
    }
   
    return self;
}


#pragma mark - lazy loading
- (UIButton *)zhBtn
{
    if (!_zhBtn)
    {
        _zhBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_zhBtn addTarget:self
                   action:@selector(zhBtnEvent)
         forControlEvents:UIControlEventTouchUpInside];
        _zhBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(15);
        [_zhBtn setTitle:@"综合"
                forState:UIControlStateNormal];
        [_zhBtn setTitleColor:MS_HEX_COLOR(0xF72F62)
                     forState:UIControlStateNormal];
    }
    
    return _zhBtn;
}
- (UIButton *)selfSupportBtn
{
    if (!_selfSupportBtn)
    {
        _selfSupportBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _selfSupportBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(14);
        [_selfSupportBtn setTitle:@"自营"
                         forState:UIControlStateNormal];
        [_selfSupportBtn setTitleColor:MS_HEX_COLOR(0x666666)
                              forState:UIControlStateNormal];
        [_selfSupportBtn addTarget:self
                            action:@selector(selfSupportBtnEvent)
                  forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _selfSupportBtn;
}

@end


#pragma mark - searchBar leftView
@interface SCSearchSearchBarLeftView ()

@property (nonatomic, strong) UIButton *typeBtn;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *textV;
@property (nonatomic, strong) UILabel *textV_label;
@property (nonatomic, strong) UIButton *textV_btn;

@end

@implementation SCSearchSearchBarLeftView


#pragma mark - event
- (void)didSelectedTypeButton
{
    if (self.didSelectedTypeButtonBlock)
    {
        self.didSelectedTypeButtonBlock(self, self.sourceInfo);
    }
}
- (void)didSelectedTextLabel
{
    if (self.didSelectedTextLabelBlock)
    {
        if (self.didSelectedTextLabelBlock(self, self.sourceInfo, self.text))
        {
            self.text = @"";
        }
    }
}
- (void)didSelectedClearButton
{
    if (self.didSelectedClearButtonBlock)
    {
        self.didSelectedClearButtonBlock(self, self.sourceInfo);
        self.text = @"";
    }
}



#pragma mark - setter
- (void)setText:(NSString *)text
{
    _text = text;
    self.textV_label.text = _text;
    self.showTextView = !ms_stringIsEmpty(_text);
}
- (void)setSourceInfo:(NSDictionary *)sourceInfo
{
    _sourceInfo = sourceInfo;
    [self.typeBtn setTitle:_sourceInfo[@"title"]
                  forState:UIControlStateNormal];
    UIImage *img = self.selectingTypeButton ? MS_IMAGE_NAMED(@"sort_up") : MS_IMAGE_NAMED(@"sort_down");
    [self.typeBtn setImage:img
                  forState:UIControlStateNormal];
}
- (void)setSelectingTypeButton:(BOOL)selectingTypeButton
{
    _selectingTypeButton = selectingTypeButton;
    [self.typeBtn setTitle:self.sourceInfo[@"title"]
                  forState:UIControlStateNormal];
    UIImage *img = _selectingTypeButton ? MS_IMAGE_NAMED(@"sort_up") : MS_IMAGE_NAMED(@"sort_down");
    [self.typeBtn setImage:img
                  forState:UIControlStateNormal];
}
- (void)setShowTypeButton:(BOOL)showTypeButton
{
    _showTypeButton = showTypeButton;
    self.typeBtn.hidden = !_showTypeButton;
    self.lineView.hidden = !_showTypeButton;
    ms_updateViewLayout(self);
}
- (void)setShowTextView:(BOOL)showTextView
{
    _showTextView = showTextView;
    self.textV.hidden = !showTextView;
    ms_updateViewLayout(self);
}


#pragma mark - UI
- (void)updateConstraints
{
    [super updateConstraints];
    [self.typeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.bottom.equalTo(self);
        make.width.mas_equalTo(55);
    }];
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(3);
        make.left.mas_equalTo(55);
        make.width.mas_equalTo(0.5);
        make.centerY.equalTo(self);
    }];
    [self.textV mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        if (self.typeBtn.isHidden)
        {
            make.left.mas_equalTo(16);
        }
        else
        {
            make.left.equalTo(self.lineView.mas_right).offset(16);
        }
        make.right.equalTo(self);
        if (self.textV.isHidden)
        {
            make.width.mas_lessThanOrEqualTo(0).priorityHigh();
        }
        else
        {
            if (self.typeBtn.isHidden)
            {
                make.width.mas_lessThanOrEqualTo(MS_SCREEN_WIDTH - 100 - 32);
            }
            else
            {
                make.width.mas_lessThanOrEqualTo(MS_SCREEN_WIDTH - 100 - 87);
            }
        }
        make.height.mas_equalTo(22);
        make.centerY.equalTo(self);
    }];
    [self.textV_label mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(8);
        make.right.equalTo(self.textV_btn.mas_left);
        make.centerY.equalTo(self.textV);
    }];
    [self.textV_btn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        if (!self.textV.isHidden)
        {
            make.right.mas_equalTo(-1.5).priorityHigh();
        }
        make.width.height.mas_equalTo(16);
        make.centerY.equalTo(self.textV);
    }];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = UIColor.clearColor;
        [self addSubview:self.typeBtn];
        [self addSubview:self.lineView];
        [self addSubview:self.textV];
        [self.textV addSubview:self.textV_label];
        [self.textV addSubview:self.textV_btn];
    }
    
    return self;
}


#pragma mark - getter
- (UIButton *)typeBtn
{
    if (!_typeBtn)
    {
        _typeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _typeBtn.frame = CGRectMake(0, 0, 62, 39);
        _typeBtn.titleLabel.font = MS_SYSTEM_FONT_OF_SIZE(13);
        [_typeBtn setTitle:@"商品"
                            forState:UIControlStateNormal];
        [_typeBtn setTitleColor:MS_HEX_COLOR(0x333333)
                                 forState:UIControlStateNormal];
        [_typeBtn setImage:MS_IMAGE_NAMED(@"sort_down")
                                     forState:UIControlStateNormal];
        [_typeBtn ms_makeChains:^(MSChainMaker * _Nonnull make) {
            
            make.ms_buttonType(MSButtonModelTextAndImgHorizontal);
            make.ms_space(2.5);
        }];
        _typeBtn.hidden = YES;
        [_typeBtn addTarget:self
                               action:@selector(didSelectedTypeButton)
                     forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _typeBtn;
}
- (UIView *)lineView
{
    if (!_lineView)
    {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(55, 3, 0.5, 24)];
        _lineView.backgroundColor = MS_HEX_COLOR(0xCAC9C9);
        _lineView.hidden = YES;
    }
    
    return _lineView;
}
- (UIView *)textV
{
    if (!_textV)
    {
        _textV = [[UIView alloc] init];
        _textV.backgroundColor = MS_HEX_COLOR(0xF2F2F2);
        _textV.hidden = YES;
        [_textV ms_makeChains:^(MSChainMaker * _Nonnull make) {
            
            make.ms_cornerRadii(11);
        }];
    }
    
    return _textV;
}
- (UILabel *)textV_label
{
    if (!_textV_label)
    {
        _textV_label = [[UILabel alloc] init];
        _textV_label.font = MS_SYSTEM_FONT_OF_SIZE(13);
        _textV_label.textColor = MS_HEX_COLOR(0x333333);
        _textV_label.lineBreakMode = NSLineBreakByTruncatingMiddle;
        _textV_label.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                              action:@selector(didSelectedTextLabel)];
        [_textV_label addGestureRecognizer:tap];
    }
    
    return _textV_label;
}
- (UIButton *)textV_btn
{
    if (!_textV_btn)
    {
        _textV_btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_textV_btn setImage:MS_IMAGE_NAMED(@"close")
                                       forState:UIControlStateNormal];
        [_textV_btn addTarget:self
                       action:@selector(didSelectedClearButton)
             forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _textV_btn;
}

@end
